import { h, ref, reactive } from 'vue'
import TableAction from '@/components/DataTable/tools/Action.vue'
import TableTags from '@/components/DataTable/tools/Tags.vue'
import { taskDel, getTaskDetail, toDoing } from '@/api/task/index'
import { useUserStore } from '@/store/modules/user'
import { router } from '@/router/index'
import { TASK_TYPE, TASK_STATUS, TASK_INSPECTS, TASK_AIRPORT_STATUS_tag } from '@/utils/dictionary.js'

const roleType = ref(null)
setTimeout(() => {
  roleType.value = useUserStore().userInfo.type
})

/* 注册table */
const tableRef = ref()
const searchParams = ref()
const userDeptId = ref(null)
const data = reactive({
  tableRef,
  searchParams,
  rowData: {},
  modalType: 'create',
  modalShow: false,
  showDraw: false,
  detail: {},
  playLiveVideo: false,
  drawTitle: '',
  auditDraw: false,
  taskId: null,
  setUserDetpId,
  handleSearch,
  editHandle,
  deleteData,
  columns: [
    {
      title: '序号',
      key: 'key',
      render: (_, index) => {
        return `${index + 1}`
      },
      align: 'center',
      width: 80
    },
    {
      title: '任务名称',
      key: 'name',
      align: 'center'
    },
    {
      title: '巡检时间',
      key: 'inspectionTime',
      align: 'center'
    },
    {
      title: '机场',
      key: 'airportName',
      align: 'center'
    },
    {
      title:'航线名称',
      key:'inspectionLineName',
      align: 'center'
    },
    {
      title: '巡检方式',
      key: 'inspectionType',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.inspectionType,
          filters: TASK_INSPECTS
        })
      }
    },
    // {
    //   title: '巡检路线',
    //   key: 'inspectionLineName',
    //   align: 'center'
    // },
    {
      title: '任务状态',
      key: 'status',
      align: 'center',
      render(row) {
        return h(TableTags, {
          data: row.status,
          filters: TASK_AIRPORT_STATUS_tag
        })
      }
    },
    {
      title: '操作',
      align: 'center',
      width: 150,
      fixed: 'right',
      render(row) {
        return h(TableAction, {
          actions: [
            // {
            //   label: '立即执行',
            //   type: 'popconfirm',
            //   auth: 'task:allTask:doingNow',
            //   tip: '确定立即执行吗?',
            //   props: {
            //     onPositiveClick: doingNow.bind(null, row.id)
            //   },
            //   ButtonProps: {
            //     text: true,
            //     type: 'primary'
            //   },
            //   show: row.status === 5 && row.inspectionType === 2 && row.execute
            // },
            {
              label: '直播',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: liveStreaming.bind(null, row, 'live')
              },
              show: row.status === 10 || row.status === 6,
              auth: 'task:allTask:liveStreaming'
            },
            {
              label: '回放',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: playback.bind(null, row, 'preview')
              },
              show: row.status === 15,
              auth: 'task:allTask:playback'
            },
            {
              label: '详情',
              type: 'button',
              props: {
                type: 'primary',
                text: true,
                onClick: getRowData.bind(null, row, 'preview')
              },
              auth: 'task:allTask:preview'
            },
            // {
            //   label: '问题详情',
            //   type: 'button',
            //   props: {
            //     type: 'primary',
            //     text: true,
            //     onClick: handleQuestion.bind(null, row, 'preview')
            //   },
            //   show: row.problemDetails,
            //   auth: 'task:allTask:questionPreview'
            // },
            // {
            //   label: '问题核实',
            //   type: 'button',
            //   props: {
            //     type: 'primary',
            //     text: true,
            //     onClick: handleQuestion.bind(null, row, 'handle')
            //   },
            //   // 超级管理员不可以，子部门的不可以
            //   show: row.problemVerify && row.status > 7 && row.deptId === userDeptId.value,
            //   auth: 'task:allTask:questionAudit'
            // },

            // {
            //   label: '重新提交',
            //   type: 'button',
            //   auth: 'task:allTask:renew',
            //   tip: '确定要重新提交吗?',
            //   props: {
            //     type: 'primary',
            //     text: true,
            //     onClick: editHandle.bind(null, row, 'update')
            //   },
            //   // 超级管理员不可以，巡检方式为机场巡逻，有失败的时候
            //   show: row.inspectionType === 2 && row.resubmit
            // },
            {
              label: '编辑',
              type: 'button',
              auth: 'task:allTask:edit',
              props: {
                type: 'primary',
                text: true,
                onClick: editHandle.bind(null, row, 'edit')
              },
              // 1、本部门管理员和创建此任务的用户，row.edit
              // 2、可在状态为任务待飞行时进入详情页编辑修改任务信息，row.status === 5
              // 3、机场起飞任务可修改信息；row.inspectionType === 2
              // 可修改信息包括：【任务地点，航段，巡检机场，巡检路线，巡检时间】必须修改起飞时间才可保存修改信息，执行后不可修改。
              show: row.status === 5 && row.inspectionType === 2
            },
            {
              label: '删除',
              type: 'popconfirm',
              auth: 'task:allTask:delete',
              tip: '确定删除这条数据吗?',
              props: {
                onPositiveClick: deleteData.bind(null, row.id)
              },
              ButtonProps: {
                text: true,
                type: 'primary'
              },
              // 5 待飞行 7 任务失败   可以,
              show: row.status=== 5 || row.status=== 7
            },
          ],
          align: 'center'
        })
      }
    }
  ]
})

function setUserDetpId(id) {
  userDeptId.value = id
}

function handleSearch(params) {
  searchParams.value = {
    name: params?.name,
    beginTime: params?.occurrenceTime?.[0],
    endTime: params?.occurrenceTime?.[1]
  }
  tableRef.value.reFetch({ searchParams })
}

/**
 * @description: 详情
 * @param {*} row 单行数据
 * @param {*} type 操作类型 create:创建，preview:预览，edit:编辑
 * @return {*}
 */
async function getRowData(row, type) {
  getTaskDetail(row.id).then(res => {
    if (res.code === 0) {
      data.detail = res.data
      data.rowData = res.data
      data.modalType = type
      data.modalShow = true
    }
  })
}
/**
 * @description: 编辑 （重新提交）
 * @return {*}
 */
function editHandle(row, type) {
  data.taskId = row.id
  router.push({
    path: '/home',
    query: {
      type: 'edit',
      taskId: data.taskId,
      flag: true
    }
  })
  // getTaskDetail(row.id).then(res => {
  //   if (res.code === 0) {
  //     data.rowData = row
  //     data.modalType = type
  //     data.modalShow = true
  //   }
  // })
}

/**
 * @description: 删除
 * @param {Array} ids id集合
 * @return {*}
 */
function deleteData(ids) {
  taskDel(ids).then((res) => {
    if (res.code === 0) {
      handleSearch()
    } else {
      $message.error(res.msg)
    }
  }).catch(e => {
    console.log(e)
  })
}

// 立即执行
function doingNow(ids) {
  toDoing(ids).then(res => {
    if (res.code === 0) {
      handleSearch()
    } else {
      $message.error(res.msg)
    }
  }).catch(e => {
    console.log(e)
  })
}

// 直播
function liveStreaming(row, type) {
  // data.drawTitle = '直播'
  // data.showDraw = true
  // data.playLiveVideo = true
  data.rowData = row
  router.push({
    path: '/home',
    query: {
      type: type,
      taskId: row?.id
    }
  })
}

// 回放
function playback(row, type) {
  // data.drawTitle = '回放'
  // data.playLiveVideo = false
  // data.showDraw = true
  data.rowData = row
  router.push({
    path: '/home',
    query: {
      type: type,
      taskId: row?.id
    }
  })
}

// 问题 详情/核实
function handleQuestion(row, type) {
  data.rowData = {
    ...row,
    type
  }
  data.auditDraw = true
}

export default data
